/*
 * Copyright 2010 Marcin Blazejewski
 *
 * This file is part of Nofate.
 *
 * Nofate is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Nofate is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Nofate. If not, see <http://www.gnu.org/licenses/>.
 *
 */


#include "font.h"
#include <string.h>
#include <SDL/SDL_image.h>

SDL_Surface *font[256];

int font_init()
{
    int rc = 0;
    SDL_Surface *_tmpfont = NULL;
    
    if (rc == 0)
    {
	    //rc = TTF_Init();
	    _tmpfont = IMG_Load("data/terminal.png");
	    if (_tmpfont == NULL) 
	    {
	        fprintf(stderr, "RE: Error: load font image\n");
	        rc = -1;
        }
    }
    
    if (rc == 0)
    { 
        int id=0;
        int x, y;                  
                
        memset(font, 0, 16 * 16 * sizeof(SDL_Surface*));
        
        for (x=0; x<16; x++)
            for (y=0; y<16; y++)       
        {
            font[id] = SDL_CreateRGBSurface(
                SDL_SWSURFACE,
                CELL_SIZE,
                CELL_SIZE,
                screen->format->BitsPerPixel,
                screen->format->Rmask,
                screen->format->Gmask,
                screen->format->Bmask,
                screen->format->Amask);
                
            if (font[id] == NULL) 
            {
                printf("RE: Error: Create surface for font glyph %d\n", id);
                continue;
            }
            
            SDL_Rect r = { x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE };
            SDL_BlitSurface(_tmpfont, &r, font[id], NULL);
            
            id++;            
        }
    }

    if (_tmpfont != NULL) SDL_FreeSurface(_tmpfont);
    
    return rc;
}

void font_end()
{
    int i;
    for (i = 0; i < 64; i++) 
    {
        if (font[i]) 
        {
            SDL_FreeSurface(font[i]);
        }
    }
}

void re_putchar(SDL_Surface* surface, int tx, int ty, int id, Uint32 fg, Uint32 bg)
{
    // background
    SDL_Rect r = { tx * CELL_SIZE, ty * CELL_SIZE, CELL_SIZE, CELL_SIZE };
    SDL_FillRect(surface, &r, bg );
    
    if ((id < 0) || (id >= 256)) return;
    
    // lock sufraces
    if (SDL_MUSTLOCK(surface)) SDL_LockSurface(surface);
    if (SDL_MUSTLOCK(font[id])) SDL_LockSurface(font[id]);
    
    // pixel pointers
    Uint32 *dst = (Uint32*)surface->pixels;
    Uint32 *src = (Uint32*)font[id]->pixels;
    
    // src coords and offsets
    int x, y;
    int srcpix = 0;
    int offset = (ty * CELL_SIZE * surface->pitch/4) + tx * CELL_SIZE;
    
    // draw source pixels on destination surface using fg color
    for (x = 0; x < CELL_SIZE; x++)
        for (y = 0; y < CELL_SIZE; y++)
    {
        if (src[ srcpix++ ] != 0)
        {
            dst[ offset + (x * surface->pitch/4) + y ] = fg;
        }
    }
    
    // unlock
    if (SDL_MUSTLOCK(font[id])) SDL_UnlockSurface(font[id]);
    if (SDL_MUSTLOCK(surface)) SDL_UnlockSurface(surface);
}
